package cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH;

import android.util.Log;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.staticPatientInfo.StaticSendBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateSendNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticSendDetailViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/16.
 */

public class StaticSendDetailPresenter extends BasePresenter<StaticSendDetailViewModel> {
    public StaticSendBeen mSendBeen;
    public String DJJLID;
    private ApiService mApiService;
    private MDialog waitDialog;
    @Override
    public void onCreatePresenter() {
        loadData(false);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    @Override
    public void loadData(boolean ref) {
        mSendBeen = (StaticSendBeen) activity.getIntent().getExtras().getSerializable("inHospitalPatientBeenList");
        DJJLID=activity.getIntent().getExtras().getString("DJJLID");
//        if(employeeInfoBeenList != null && employeeInfoBeenList.size() != 0){
////            PreferenceManager.getInstance().setStaticTakeProgressTotal(employeeInfoBeenList.size()+"");
//            FitstData.addAll(employeeInfoBeenList);
////            Log.e("PXY", "init: "+FitstData.toString() );
//            if(FitstData!=null ){
//                viewModel.setWData(FitstData);
//            }
//        }
    }
    public void sendUpdate(String DJCZLX,String CZYHID){
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(UpdateSendNode.getContent("PDAU06",DJJLID,"12108",DJCZLX,CZYHID,"99")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitFail("服务器异常！");
                            return;
                        }
                        mOnSubmitResult.submitSuccess();
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }

    //药单更新监听
    private OnSubmitHospitalResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitHospitalResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitHospitalResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }
}
